package com.spvs.job.task;


import cn.hutool.core.date.DateUtil;
import com.spvs.common.core.constant.NoticeConstant;
import com.spvs.common.core.utils.DateUtils;
import com.spvs.job.domain.*;
import com.spvs.job.mapper.EquipPatrolPlanRecordMapper;
import com.spvs.job.mapper.EquipPatrolRecordDetailMapper;
import com.spvs.job.mapper.EquipPatrolRecordMapper;
import com.spvs.system.api.IEquipNoticeService;
import com.spvs.system.api.domain.EquipNotice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 巡检记录
 *
 * @author supervision
 */
@Component
@Slf4j
public class PatrolRecord {

    @Autowired
    private EquipPatrolPlanRecordMapper patrolPlanRecordMapper;

    @Autowired
    private EquipPatrolRecordMapper patrolRecordMapper;

    @Autowired
    private EquipPatrolRecordDetailMapper equipPatrolRecordDetailMapper;

    @Autowired
    private IEquipNoticeService equipNoticeService;

    public void executeTasks() {
        // log.info("巡检计划任务。。。。");
        List<EquipPatrolPlanRecord> equipPatrolPlanRecords = patrolPlanRecordMapper.
                selectAll(CommonEnum.YOU_XIAO.getCode());
        if (CollectionUtils.isEmpty(equipPatrolPlanRecords)) {
            // log.info("未查询到巡检计划。。。。");
            return;
        }
        log.info("查询到巡检计划任务{}条。。。。", equipPatrolPlanRecords.size());
        for (EquipPatrolPlanRecord equipPatrolPlanRecord : equipPatrolPlanRecords) {
            Long planRecordId = equipPatrolPlanRecord.getId();
            Integer taskType = equipPatrolPlanRecord.getTaskType();
            UpkeepEnum byFormCode = UpkeepEnum.getByFormCode(equipPatrolPlanRecord.getCycle());
            List<EquipPatrolRecordDetail> equipPatrolRecordDetails =
                    equipPatrolRecordDetailMapper.selectEquipPatrolRecordDetailByPlanId(planRecordId);
            if (CollectionUtils.isEmpty(equipPatrolRecordDetails)) {
                log.info("{}未查询到绑定设备详情信息。。。", equipPatrolPlanRecord.getName());
                continue;
            }
            labe:
            for (EquipPatrolRecordDetail recordDetail : equipPatrolRecordDetails) {
                EquipPatrolRecord equipPatrolRecord =
                        patrolRecordMapper.selectEquipPatrolRecordByCode(recordDetail.getDeviceId(), taskType, 0);
                if (equipPatrolRecord != null) {
                    String days = DateUtils.dayDiff(equipPatrolRecord.getPlanPatrolTime(), new Date());
                    if (Integer.parseInt(days) < Integer.parseInt(byFormCode.getCode())) continue labe;
                    extracted(planRecordId, recordDetail);
                } else {
                    extracted(planRecordId, recordDetail);
                }
            }
        }
    }

    private void extracted(Long planRecordId, EquipPatrolRecordDetail recordDetail) {
        EquipPatrolPlanRecord planRecord = patrolPlanRecordMapper.selectEquipPatrolPlanRecordById(planRecordId);
        EquipPatrolRecord upkeepRecord = new EquipPatrolRecord();
        upkeepRecord.setPlanId(planRecordId);
        upkeepRecord.setDeviceId(recordDetail.getDeviceId());
        upkeepRecord.setEquipmentCode(recordDetail.getEquipmentCode());
        upkeepRecord.setEquipmentName(recordDetail.getEquipmentName());
        upkeepRecord.setPlanPatrolTime(DateUtil.beginOfDay(new Date()));
        upkeepRecord.setLiablePerson(recordDetail.getLiablePerson());
        upkeepRecord.setDeptId(recordDetail.getDeptId());
        upkeepRecord.setState(0);
        upkeepRecord.setType(0);
        upkeepRecord.setTaskType(planRecord != null ? planRecord.getTaskType() : 0);
        patrolRecordMapper.insertEquipPatrolRecord(upkeepRecord);
        EquipNotice equipNotice = new EquipNotice();
        equipNotice.setContent("您有一条巡检任务有待执行，请及时查看");
        equipNotice.setTitle(NoticeConstant.TITLE_2);
        equipNotice.setStatus(0L);
        equipNotice.setDeptId(recordDetail.getDeptId());
        equipNotice.setType(0L);
        equipNotice.setRemark(upkeepRecord.getId().toString());
        if (recordDetail.getLiablePerson() != null) {
            equipNotice.setType(1L);
            equipNotice.setUserId(Long.valueOf(recordDetail.getLiablePerson()));
        }
        equipNoticeService.insertEquipNotice(equipNotice);
    }
}


